package com.haoshuang.zhouzhoubang.ui.page.shop.child

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.SizeUtils
import com.haoshuang.zhouzhoubang.databinding.FragmentShoppingChildBinding
import com.haoshuang.zhouzhoubang.model.bean.ShopBean
import com.haoshuang.zhouzhoubang.ui.page.shop.detail.ProductDetailsActivity
import com.haoshuang.zhouzhoubang.widget.GridSpaceItemDecoration
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast


/**
 *  商城里的子界面
 */
class ShoppingChildFragment(val info: Int) :
    BaseVMFragment<ShoppingChildViewModel, FragmentShoppingChildBinding>() {
    val TAG = "ShoppingChildFragment"
    val spanCont: Int = 2 // 商品的recyclerView每行展示的个数
    lateinit var rvShoppingRecyclerViewAdapter: ShoppingRecyclerViewAdapter

    /*商品列表*/
    var goodsList = mutableListOf<ShopBean>()

    companion object {
        /**
         * 创建商城里的子界面的实例对象 */
        fun newInstance(info: Int): ShoppingChildFragment {
            return ShoppingChildFragment(info)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        /*网格布局*/
        val layoutManager = GridLayoutManager(context, spanCont)
        binding.icGoodsList.recyclerView.layoutManager = layoutManager

        rvShoppingRecyclerViewAdapter = ShoppingRecyclerViewAdapter(goodsList)

        binding.icGoodsList.recyclerView.adapter = rvShoppingRecyclerViewAdapter
        rvShoppingRecyclerViewAdapter.setOnItemClick { view, position, item ->
            startPage<ProductDetailsActivity>("ids" to goodsList[position].id)
        }
        rvShoppingRecyclerViewAdapter.setEmptyData()
        /*设置recyclerView中item的上下左右的间距*/
        binding.icGoodsList.recyclerView.addItemDecoration(
            GridSpaceItemDecoration(spanCont, SizeUtils.dp2px(4.5f), SizeUtils.dp2px(4.5f))
        )

        /*下拉刷新*/
        binding.icGoodsList.smartRefreshLayout.setOnRefreshListener {
            p = 1
            initGoodsList()
        }
        /*上滑加载*/
        binding.icGoodsList.smartRefreshLayout.setOnLoadMoreListener {
            p++
            initGoodsList()
        }

    }

    override fun initData() {
        initGoodsList()
    }

    private fun initGoodsList() {
        vm.getData("cate_id" to info, "page" to p, "display_platform" to 3)
    }

    override fun lazyLoadData() {

    }

    override fun startObserve() {
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.icGoodsList.smartRefreshLayout.setupEmptyData(goodsList, rvShoppingRecyclerViewAdapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.icGoodsList.smartRefreshLayout.setupData(it,goodsList, rvShoppingRecyclerViewAdapter, p)
            }
        }

    }

    fun updata() {
        p = 1
        initGoodsList()
    }

}